<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :obj="companyInfo" :is-root="true" @add-dept="onAdd" />
        <!-- 树形控件 -->
        <el-tree :data="treeData" :props="defaultProps" :default-expand-all="true">
          <tree-tools
            slot-scope="{data}"
            :obj="data"
            @del-btn="loadDepartment"
            @obj="onAdd"
            @add-dept="onAdd"
            @edit-dept="onEdit"
          />
        </el-tree>
      </el-card>
    </div>
    <add-depts ref="depts" v-model="addDialgVisible" :node="treeNode" @add-btn="loadDepartment" @edit-btn="loadDepartment" />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import { getDepartmentsList } from '@/api/departments'
import { transListToTree } from '@/utils/index'
import AddDepts from './components/add-dept.vue'
export default {
  components: { TreeTools, AddDepts },
  data() {
    return {
      treeData: [],
      treeNode: {},
      defaultProps: {
        label: 'name'
      },
      companyInfo: { name: '传智播客', manager: '负责人', id: '' },
      addDialgVisible: false
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    // 加载数据
    async loadDepartment() {
      const res = await getDepartmentsList()
      this.treeData = transListToTree(res.depts, '')
    },
    // 点击添加子部门
    onAdd(data) {
      this.addDialgVisible = true
      console.log(data)
      this.treeNode = data
    },
    onEdit(data) {
      // 显示弹层
      this.addDialgVisible = true
      // 传给子组件 做校验
      this.treeNode = data
      // 获取最新的数据渲染到form表单
      this.$refs.depts.getDeptsInfo(data.id)
      console.log('编辑')
    }
  }
}
</script>

<style>
.el-row {
  width: 100%;
}
</style>
